package com.happyfamily.springboot.crowdsourcingplatform.controller.pan;

import com.happyfamily.springboot.crowdsourcingplatform.annotation.OperationLogDetail;
import com.happyfamily.springboot.crowdsourcingplatform.base.result.PageTableRequest;
import com.happyfamily.springboot.crowdsourcingplatform.base.result.Results;
import com.happyfamily.springboot.crowdsourcingplatform.enums.OperationType;
import com.happyfamily.springboot.crowdsourcingplatform.enums.OperationUnit;
import com.happyfamily.springboot.crowdsourcingplatform.model.Need;
import com.happyfamily.springboot.crowdsourcingplatform.service.pan.DemandService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextImpl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author Administrator
 * @create 2020/02/26 10:29
 */
@Controller
@RequestMapping("api-front/demand")
public class DemandController
{
	@Autowired
	private DemandService demandService;


	@GetMapping("/list")
	@ResponseBody
	@ApiOperation(value = "需求数据获取", notes = "查询类型是全表查询", httpMethod = "GET", response = Results.class)
	@OperationLogDetail(detail = "需求数据获取", level = 1, operationUnit = OperationUnit.USER, operationType = OperationType.SELECT)
	public Results<Need> getAllNeedsByPage(PageTableRequest request)
	{
		request.countOffset();
		return demandService.getNeedBys(request.getOffset(), request.getLimit());
	}


	@GetMapping("/findNeedByFuzzyNeedTitle")
	@ResponseBody
	@ApiOperation(value = "需求数据获取", notes = "查询类型是全表查询", httpMethod = "GET", response = Results.class)
	@OperationLogDetail(detail = "需求数据获取", level = 1, operationUnit = OperationUnit.USER, operationType = OperationType.SELECT)
	public Results<Need> getNeedByFuzzyNeedTitle(PageTableRequest requests, String ntitle, String sname)
	{

		requests.countOffset();
		return demandService.getNeedByFuzzyNeedTitle(ntitle, sname, requests.getOffset(), requests.getLimit());
	}





	@GetMapping("/userNeed")
	@ResponseBody
	@ApiOperation(value = "个人需求数据获取", notes = "查询类型是用户账号", httpMethod = "GET", response = Results.class)
	@OperationLogDetail(detail = "个人需求数据获取", level = 1, operationUnit = OperationUnit.USER, operationType = OperationType.SELECT)
	public Results<Need> getUserNeedsByPage(PageTableRequest pag, HttpServletRequest request)
	{
		pag.countOffset();
		SecurityContextImpl securityContextImpl = (SecurityContextImpl)

				request.getSession().getAttribute("SPRING_SECURITY_CONTEXT");
		//获得你登录的账号
		String account = securityContextImpl.getAuthentication().getName();
		return demandService.getUserNeedBys(pag.getOffset(), pag.getLimit(),account);
	}



	@GetMapping("/findNeedByUserSearch")
	@ResponseBody
	@ApiOperation(value = "需求数据获取", notes = "查询类型是全表查询", httpMethod = "GET", response = Results.class)
	@OperationLogDetail(detail = "需求数据获取", level = 1, operationUnit = OperationUnit.USER, operationType = OperationType.SELECT)
	public Results<Need> getfindNeedByUserSearch(PageTableRequest pag, String ntitle, String sname, HttpServletRequest request)
	{

		pag.countOffset();

		SecurityContextImpl securityContextImpl = (SecurityContextImpl)

				request.getSession().getAttribute("SPRING_SECURITY_CONTEXT");
		//获得你登录的账号
		String account = securityContextImpl.getAuthentication().getName();
		return demandService.getNeedByUserSearch(ntitle, sname, pag.getOffset(), pag.getLimit(),account);
	}










}
